package org.jackysoft.service.impl;

import java.util.Collection;

import javax.annotation.Resource;

import org.apache.commons.collections.Predicate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jackysoft.entity.Variable;
import org.jackysoft.entity.WorkflowSetting;
import org.jackysoft.entity.WorkflowSettingPK;
import org.jackysoft.service.AbstractServiceImpl;
import org.jackysoft.service.VariableService;
import org.jackysoft.service.WorkflowService;
import org.jackysoft.service.WorkflowSettingService;
import org.jackysoft.util.CollectionUtils;
import org.jackysoft.util.StringUtils;
import org.jackysoft.util.WorkflowUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class WorkflowSettingServiceImpl extends
AbstractServiceImpl<WorkflowSettingPK,WorkflowSetting> implements
		WorkflowSettingService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8580492501846574855L;
    private VariableService varService;
	private WorkflowService workflowService;
	
	public WorkflowSettingServiceImpl() {
		super(WorkflowSetting.class);
	}


	@Override
	public void create(Collection<WorkflowSetting> sts)
			throws DataAccessException {
		CollectionUtils.filter(sts,new WorkflowSettingNotSettingPredicate());
		for(WorkflowSetting ws : sts){
			WorkflowSetting nws = this.get(ws.getId());
			if(nws!=null) remove(nws);	
			create(ws);
		}
		
	}

	

	@Override
	public WorkflowSetting get(WorkflowSettingPK id)
			throws DataAccessException {
		return super.get(id);
	}

	
	
	
	
	@Override
	public Collection<WorkflowSetting> getsByName(String workflowName)
			throws DataAccessException {
		Session sess = getSession();
		Query q = sess.createQuery("from "+this.table()+" w where w.id.workflowName=:name");
		return q.list();
	}

	/**
	 * @return 组合后的工作流设置对象
	 **/
	@Override
	public WorkflowSetting combine(
			Long entityId,
			String workflowName, 
			int actionId,
			Long stepId) throws DataAccessException {
		WorkflowSettingPK id = new WorkflowSettingPK(workflowName,stepId,actionId);
		WorkflowSetting wset = get(id);
		if(wset!=null){
			Variable userValue = varService.get(entityId, wset.getUser());
		    if(userValue!=null){
		    	wset.setUser(WorkflowUtil.getVarStringValue(userValue));
		    }
		}
		return wset;
	}

	@Resource
	public void setVarService(VariableService varService) {
		this.varService = varService;
	}
	class WorkflowSettingNotSettingPredicate implements Predicate {

		@Override
		public boolean evaluate(Object object) {
			if(object==null)return false;
			WorkflowSetting s = (WorkflowSetting) object;
			return s.getRoles().size()>0 || s.getDepartments().size()>0 || (s.getUser()!=null&&StringUtils.isNotVoid(s.getUser()));
		}
		
	}
	@Override
	public WorkflowSetting get(long workflowId, int actionId, int stepId)
			throws DataAccessException {
		String workflowName = workflowService.findEntry(workflowId).getWorkflowName();
		WorkflowSettingPK id = new WorkflowSettingPK(workflowName,new Integer(stepId).longValue(),actionId);
		WorkflowSetting ws = get(id);
		return ws;
	}

    @Resource
	public void setWorkflowService(WorkflowService workflowService) {
		this.workflowService = workflowService;
	}


	@Override
	@Resource(name = "idtSessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
    
    
    
}
